package org.september.pisces.menuframe.vo;

import java.util.ArrayList;
import java.util.List;

public class MenuTree {

	private List<SimpleMenu> root = new ArrayList<>();
	private String groupLabel;
	
	public List<SimpleMenu> getRoot() {
		return root;
	}
	
	public void addMenu(String text ,String url,String icon) {
		addMenu(text, url, icon,false);
	}
	
	public MenuTree clone() {
		MenuTree mt = new MenuTree();
		for(SimpleMenu menu: this.root) {
			mt.root.add(menu.simpleClone());
		}
		mt.groupLabel = this.groupLabel;
		return mt;
	}
	/**
	 * 
	 * @param text 格式：一级菜单-二级菜单-三级菜单
	 * @param url
	 * @param open
	 */
	public void addMenu(String text ,String url ,String icon,boolean open) {
		String[] arr = text.split("-");
		List<SimpleMenu> parent = root;
		for(int i=0;i<arr.length;i++) {
			String mText = arr[i];
			SimpleMenu menu = findMenu(parent,mText);
			if(menu==null) {
				menu = new SimpleMenu();
				menu.setText(mText);
				menu.setIconClass(icon);
				if(i==arr.length-1) {
					menu.setUrl(url);
					menu.setAutoOpen(open?1:0);
				}
				parent.add(menu);
			}
			parent = menu.getChildren();
		}
	}

	private SimpleMenu findMenu(List<SimpleMenu> parent, String mText) {
		for(SimpleMenu sm : parent) {
			if(sm.getText().equals(mText)) {
				return sm;
			}
		}
		return null;
	}

	public String getGroupLabel() {
		return groupLabel;
	}

	public void setGroupLabel(String groupLabel) {
		this.groupLabel = groupLabel;
	}
	
}
